<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt />
        <p>电商后台管理系统</p>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width>
        <div class="toggle-button" @click="toggle">|||</div>
        <el-menu
          :default-active="defaultActive"
          :collapse="flage"
          class="el-menu-vertical-demo"
          :unique-opened="true"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse-transition="false"
          :router="true"
        >
          <el-submenu v-for="(item1,index) in menusList" :key="index" :index="'/'+item1.path">
            <template slot="title">
              <i style="margin-right:5px;" :class="iconsObj[item1.id]"></i>
              <span>{{item1.authName}}</span>
            </template>
            <el-menu-item-group
              v-for="(item2,index) in item1.children"
              :key="index"
              :index="'/'+item2.path"
            >
              <el-menu-item :index="'/'+item2.path" @click="handleColor('/'+                                                                                                                          item2.path)">
                <i class="el-icon-menu"></i>
                {{item2.authName}}
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>
           
<script>
import { menus_api } from "../api/index";
export default {
  created() {
    //执行请求左侧菜单数据的函数
    this.getMenus();
  },
  data() {
    return {
      flage: true,
      //  默认展开导航
      defaultActive: window.sessionStorage.getItem("defaultActive") || 'users',
      menusList: [],
      iconsObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao",
      },
    };
  },
  methods: {
    exit() {
      //清除token
      window.sessionStorage.setItem("token", null);
      //跳转到登录页面
      this.$router.push("/login");
    },
    //退出按钮
    logout() {
      //清除token
      window.sessionStorage.setItem("token", null);
      //跳转到登录页面
      this.$router.push("/login");
    },
    //左侧菜单
    handleColor(key) {
      // console.log(key, keyPath);
      //   点击导航菜单需要做两件事
      /* 
      1、获取当前点击菜单的path值  ====index
      2、将获取的值做本地缓存
      
      */
      this.defaultActive = key;
      window.sessionStorage.setItem("defaultActive", key);
    },
    //获取左侧菜单数据
    async getMenus() {
      //发送请求
      const { data: res } = await menus_api();
      this.menusList = res.data;
      console.log(res.data,7777);
    },
    toggle() {
      this.flage = !this.flage;
    },
  },
};
</script>
<style scoped lang='less'>
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .logo {
      display: flex;
      align-items: center;
      justify-content: space-between;
      p {
        margin-left: 10px;
        color: white;
        font-size: 20px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      cursor: pointer;
    }
    .el-menu {
      border: 0;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>